package com.atguigu.seckill.controller;

import com.atguigu.common.utils.R;
import com.atguigu.seckill.service.SeckillService;
import com.atguigu.seckill.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author kabuda
 * @create 2023-01-13 13:38
 */
@Controller
public class SeckillController {
    @Autowired
    private SeckillService seckillService;

    /**
     * 返回当前时间可以参与的秒杀信息
     *
     * @return
     */
    @GetMapping("/getCurrentSecKillSkus")
    @ResponseBody
    public R getCurrentSecKillSkus() {
        List<SeckillSkuRedisTo> tos = seckillService.getCurrentSecKillSkus();
        return R.ok().setData(tos);
    }

    @GetMapping("/seckill/sku/{skuId}")
    @ResponseBody
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId) {
        SeckillSkuRedisTo to = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().setData(to);
    }

    @GetMapping("/kill")
    public String secKill(@RequestParam("killId") String killId, @RequestParam("key") String key, @RequestParam("num") Integer num, Model model) {
        String orderSn = seckillService.kill(killId, key, num);
        model.addAttribute("orderSn", orderSn);
        return "success";
    }
}

